package czopyk.model.request;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author czopson_
 *
 */
public class Request {

	private static final AtomicInteger ai = new AtomicInteger(0);
	
	private final int id = ai.getAndIncrement();
	
	protected static Logger log = Logger.getLogger("Request");

	protected final LoadClass lc;

	private boolean done = false;

	public Request(LoadClass lc) {
		super();
		this.lc = lc;
	}
	
	public boolean isDone() {
		return done;
	}

	public void done(String centername) {
		log.debug(this + " done by '" + centername + "'");
		done = true;
	}

	private Request parent;
	
	public long getId() {
		return id;
	}

	// najnizszy z numerow centrow, ktorze przetwarzaja request
	private Set<Integer> indexes;
	
	@Override
	public String toString() {
		return toString(2);
	}
	
	public String toString(int depth) {
		StringBuilder sb = new StringBuilder("Request " + getId() + " ("
				+ lc + ")"
				+ (this instanceof CompositeRequest ? " composite" : " elemental")
				+ "; done = " + done + "\n");
		if (this instanceof CompositeRequest) {
			for (Request r : ((CompositeRequest)this).getComponents()) {
				sb.append(StringUtils.repeat("  ", depth) + "-> " + r.toString(depth + 2));
			}
		}
		return sb.toString();
	}

	/**
	 * @return żądanie całkowite
	 */
	public Request getParent() {
		return parent;
	}

	public Request setParent(Request parent) {
		this.parent = parent;
		return this;
	}

	public Set<Integer> getIndexes() {
		if (indexes == null) {
			indexes = new HashSet<Integer>();
		}
		return indexes;
	}



	public LoadClass getLc() {
		return lc;
	}

	@Override
	public int hashCode() {
		return id;
	}

}
